﻿using BCCommon;
using BCEntity.TMS.RedPacket.RedPacketType;
using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BCData.TMS.RedPacket.RedPacketType
{
    public class RedPacketTypeData: IRedPacketTypeData
    {
        private readonly string SQL_QUERY = @" SELECT
		                                        A.RedPacketId,
		                                        A.RedPacketTypeId,
		                                        A.RedPacketType,
		                                        A.RedPacketAmount,
		                                        A.GrundlagenType,
		                                        A.RedPacketName,
		                                        A.RedPackCount,
		                                        A.RemainCount,
		                                        A.ReceivedCount,
		                                        A.AgentRecordId,
		                                        A.Enabled,
		                                        A.CreateAdminId,
		                                        A.CreateAdminName,
		                                        A.CreateTime,
		                                        A.UpdateTime,
		                                        A.EditAdminId,
		                                        A.EditAdminName
                                            FROM
                                                redpackettype A  ";
        private readonly IDatabaseContext database;
        public RedPacketTypeData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<IEnumerable<RedPacketTypeEntity>> GetList(bool? isEnabled)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnabled.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnabled.Value, DbType.Boolean);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sql = $"{SQL_QUERY} {whereSql} GROUP BY A.RedPacketTypeId ORDER BY A.CreateTime DESC ";
            return await this.database.Slave.QueryAsync<RedPacketTypeEntity>(sql, parameters);
        }

        public async Task<RedPacketTypeEntity> GetSingle(long redPacketTypeId)
        {
            string sql = typeof(RedPacketTypeEntity).GetSelectSqlByPrimaryKeys();
            return await database.Slave.QueryFirstOrDefaultAsync<RedPacketTypeEntity>(sql,new { redPacketTypeId });
        }

        public async Task<RedPacketTypeEntity> InsertAsync(RedPacketTypeEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<(IEnumerable<RedPacketTypeEntity>,int)> Query(bool? isEnabled, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();

            if (isEnabled.HasValue)
            {
                wheres.Add("A.Enabled = ?Enabled");
                parameters.Add("?Enabled", isEnabled.Value, DbType.Boolean);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }
            string sqlCount = " SELECT COUNT(A.RedPacketTypeId) FROM RedPacketType A  ";
            string sql = $@"{SQL_QUERY} {whereSql} GROUP BY A.RedPacketTypeId ORDER BY A.CreateTime DESC LIMIT {(pageIndex - 1) * pageSize}, {pageSize};
                            {sqlCount}{whereSql} ";
            return await this.database.Slave.QueryPageableListAsync<RedPacketTypeEntity>(sql, parameters);
        }

        public async Task<RedPacketTypeEntity> UpdateAsync(RedPacketTypeEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }
    }
}
